package org.finmanager.statistics;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;

/**
 * Created by Kelvin on 28/4/2014.
 */

//A class to control the heat map table by getting itemDistributionModel & itemDistributionUI
public class ItemDistributionController implements ActionListener {
	public static final String INCREASE_MONTH = "ItemDistributionController.INCREASE_MONTH";
	public static final String DECREASE_MONTH = "ItemDistributionController.DECREASE_MONTH";
	public static final String SET_CURRENT_MONTH = "ItemDistributionController.SET_CURRENT_MONTH";

	ItemDistributionModel itemDistributionModel;
	ItemDistributionUI itemDistributionUI;

	public ItemDistributionController(StatisticsController statisticsController) {
		itemDistributionModel = statisticsController.getStatisticsModel().getItemDistributionModel();
		itemDistributionUI = new ItemDistributionUI(this);

		itemDistributionUI.addButtonListener(this);

		statisticsController.addComponent(getUIMainComponent(), "Distribution");
	}

	public ItemDistributionModel getItemDistributionModel() {
		return itemDistributionModel;
	}

	public JComponent getUIMainComponent() {
		return itemDistributionUI.getMainComponent();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String ac = e.getActionCommand();
		if (ac.equals(INCREASE_MONTH)) {
			itemDistributionModel.increaseMonth();
			itemDistributionUI.setCurrentMonthText(getItemDistributionModel().getCalendar());
		}
		else if (ac.equals(DECREASE_MONTH)) {
			itemDistributionModel.decreaseMonth();
			itemDistributionUI.setCurrentMonthText(getItemDistributionModel().getCalendar());
		}
		else if (ac.equals(SET_CURRENT_MONTH)) {
			itemDistributionModel.setCalendar(Calendar.getInstance());
			itemDistributionUI.setCurrentMonthText(Calendar.getInstance());
		}
	}
}
